package com.botao.dao;

import com.botao.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @deprecated 已经由mybatis实现，不再使用jdbc模板。
 */
@Deprecated
@Repository("jdbcStudentDao")
public class StudentDaoImpl implements StudentDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(Student student) {
        String sql = "insert into student value (?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,
                student.getId(),
                student.getName(),
                student.getSex(),
                student.getCard(),
                student.getPhone(),
                student.getDepartment(),
                student.getBanji(),
                student.getBanjiId()
        );
    }

    @Override
    public int delete(String id) {
        return jdbcTemplate.update("delete from student where id = ?", id);
    }

    @Override
    public int update(Student student) {
        String sql = "update student set name = ? , sex = ? , card = ? , phone = ? , banji = ? , department = ? , banjiid = ? where id = ?";
        return jdbcTemplate.update(sql,
                student.getName(),
                student.getSex(),
                student.getCard(),
                student.getPhone(),
                student.getBanji(),
                student.getDepartment(),
                student.getBanjiId(),
                student.getId()
        );
    }

    @Override
    public List<Student> listStudent(Long currentPage, Long pageSize) {
        long start = (currentPage - 1) * pageSize;
        return jdbcTemplate.query("select * from student limit " + start + " ," + pageSize, new BeanPropertyRowMapper<Student>(Student.class));
    }

    @Override
    public Student getOne(String id) {
        List<Student> query = jdbcTemplate.query("select * from student where id = ? order by id", new BeanPropertyRowMapper<Student>(Student.class), id);
        return query.get(0);
    }

    @Override
    public Long count() {
        return jdbcTemplate.queryForObject("select COUNT(*) from student", Long.class);
    }

    @Override
    public Student searchStudent(String id) {
        Integer count = jdbcTemplate.queryForObject("select COUNT(*) from student where id = ?", Integer.class, id);
        if (count == null || count == 0) {
            return null;
        }
        return jdbcTemplate.query("select * from student where id = ?", new BeanPropertyRowMapper<Student>(Student.class), id).get(0);
    }

    @Override
    public int deleteStudentByClassesId(int id) {
        return jdbcTemplate.update("delete from student where banjiid = ?", id);
    }

    @Override
    public List<Student> getStudentsByClassesId(int id) {
        return jdbcTemplate.query(" select * from student where banjiid = ?", new BeanPropertyRowMapper<Student>(Student.class), id);
    }

    @Override
    public boolean studentExists(String id) {
        Integer integer = jdbcTemplate.queryForObject("select count(*) from student where id = ?", Integer.class, id);
        return integer != 1;
    }
}
